<template>
  <div class="Team">
    <el-button type="primary" @click="openDialog()">新增团队风采</el-button>

    <el-table border :data="tableData" v-loading="loading" style="width: 100%">
      <el-table-column prop="id" label="序号" width="180"></el-table-column>
      <el-table-column prop="img" label="图片">
        <!-- 使用 v-slot 替代 slot-scope -->
        <template #default="scope">
          <img style="width: 100%" :src="imgServer + scope.row.img" alt />
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="primary" @click="handleEdit(scope.$index, scope.row)">
            <!-- 使用 ElIcon 组件包裹图标 -->
            <el-icon>
              <Edit />
            </el-icon>
          </el-button>
          <el-button type="danger" @click="handleDelete(scope.$index, scope.row)">
            <el-icon>
              <Delete />
            </el-icon>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 使用 v-model 替代 :visible.sync -->
    <el-dialog title="团队风采编辑" v-model="dialogFormVisible">
      <el-form :model="formData">
        <el-form-item label="风采图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            :action="uploadImg"
            :headers="headers"
            :show-file-list="false"
            :on-success="handleSuccess"
            name="image"
          >
            <img
              v-if="formData.img"
              :src="imgServer + formData.img"
              class="avatar"
            />
            <i v-else style="font-size: 80px;"> <el-icon><Plus /></el-icon></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="formData.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <!-- 使用 v-slot 定义底部插槽 -->
      <template #footer>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleCreateOrModify()">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { Edit, Delete,Plus } from '@element-plus/icons-vue';

export default {
  components: {
    Edit,
    Delete,
    Plus
  },
  data() {
    return {
      loading: true,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      tableData: [],
      formData: {
        id: 0,
        img: "",
        remark: "",
        createTime: new Date(),
      },
      options: {},
      headers: {},
    };
  },
  mounted() {
    let token = "Browser " + sessionStorage.getItem("token");
    this.options = {
      headers: {
        Authorization: token,
      },
    };
    this.headers = {
      Authorization: token,
    };

    this.loadData();
  },
  methods: {
    handleSuccess(response, file, fileList) {
      this.formData.img = response.files[0];
    },
    loadData() {
      this.loading = true;
      this.$http
        .get("team/all", this.options)
        .then((response) => {
          this.tableData = response.data;
          this.loading = false;
        })
        .catch((e) => {
          // 使用 this.$message 显示错误消息
          this.$message.error("网络或程序异常！" + e);
        });
    },
    openDialog() {
      // 清除数据
      this.formData = {
        id: 0,
        img: "",
        remark: "",
        createTime: new Date()
      };
      this.dialogFormVisible = true;
    },
    // 新增
    handleCreateOrModify() {
      if (!this.formData.id) {
        // ID 无效时 视为新增
        this.loading = true;
        this.$http
          .post("team/create", this.formData, this.options)
          .then((response) => {
            this.loading = false;
            // 使用 this.$message 显示成功消息
            this.$message.success("创建成功！");
            this.dialogFormVisible = false;
            this.loadData();
          })
          .catch((e) => {
            this.$message.error("网络或程序异常！" + e);
          });
      } else {
        this.loading = true;
        this.$http
          .put("team/update", this.formData, this.options)
          .then((response) => {
            this.loading = false;
            this.$message.success("修改成功！");
            this.dialogFormVisible = false;
            this.loadData();
          })
          .catch((e) => {
            this.$message.error("网络或程序异常！" + e);
          });
      }
    },
    handleEdit(index, row) {
      this.formData = { ...row };
      this.dialogFormVisible = true;
    },
    handleDelete(index, row) {
      // 使用 this.$messageBox.confirm 弹出确认框
      this.$messageBox.confirm("此操作将永久删除此条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // 已确认删除
          // 调接口删除
          this.loading = true;
          this.$http
            .delete(`team/delete/${row.id}`, this.options)
            .then((response) => {
              this.loading = false;
              this.$message.success("删除成功！");
              this.loadData();
            })
            .catch((e) => {
              this.$message.error("网络或程序异常！" + e);
            });
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
    // 时间格式化
    dateFormat(row) {
      // row 表示一行数据, createTime 表示要格式化的字段名称
      let t = new Date(row.createTime);
      return t.getFullYear() + "-" + (t.getMonth() + 1) + "-" + t.getDate();
    }
  }
};
</script>

<style scoped>
.el-table {
  margin-top: 20px;
}
.avatar {
  width: 300px;
}
</style>